<template>
	<view>
		<o-navbarx :back="true" :isback="true" color="#000000" >
			<template #left_conent>
				<view class="bgf6 pad-lr-20 pad-tb-14 radius-36 display-sb align-items ml20 width-100" @click="gosearch">
					<image src="/static/img/icon_sousuo.png" class="proudct-item-icon-24" mode=""></image>
					<input type="text" placeholder="搜索商品名称" v-model="keyword" @confirm="seach" class="ipt fontSize-26" />
				</view>
			</template>
		</o-navbarx>
		<view class="pad-lr-16 index_bg_top">
			<view class="mar-top-16 bgfff radius-16 pad-lr-24" v-for="item in List" :key="item.id">
				<view class="display pad-tb-24 border_bottom_F1 poRe">
					<view class="poAb left-0 top-24 fontSize-22 state_tag pad-lr-6 col-f" v-if="[2,3].includes(item.status)">{{item.status==2?'审核中':'已驳回'}}</view>
					<image :src="item.image_text" class="wid-hig-140 radius-16" mode="aspectFill"></image>
					<view class="ml16 right_view">
						<view class="fontSize-30">{{item.name}}</view>
						<view class="mar-top-12 fontSize-24 col-6">已售 {{item.sales}}</view>
						<view class="col-E23737 fontBold fontSize-30 mar-top-12"><text class="fontSize-24">￥</text>{{item.price}}</view>
					</view>
				</view>
				<view class="pad-tb-24 display align-items">
					<view class="display-ce align-items width-50" @click="_shopdelGoods(item.id)">
						<image src="/static/img/icon_shanchu1.png" class="wid-hig-36" mode=""></image>
						<view class="ml24 fontSize-28 ml24">删除</view>
					</view>
					<view class="display-ce align-items width-50" style="border-left: 2rpx solid #F1F1F1;" @click.stop="_shopsetUpDown(item.id,item.status)" v-if="[0,1].includes(item.status)">
						<image src="/static/img/shangjia.png" class="wid-hig-36" mode=""></image>
						<view class="ml24 fontSize-28 ml24">{{item.status==1?'下架':'上架'}}</view>
					</view>
				</view>
			</view>
			<view class="display-ce align-items" v-if="List.length==0">
				<tm-result :showBtn="false" subTitle=" " title="暂无数据" color="#bc8c1e"></tm-result>
			</view>
		</view>
	</view>
</template>

<script lang="ts" setup>
import { reactive, ref } from 'vue';
import {shopgoodsList,shopsetUpDown,shopdelGoods} from '@/utils/api'
import { onLoad } from '@dcloudio/uni-app';
onLoad(()=>{
	_shopgoodsList()
})
const PageData = reactive({
	page:1,
	last_page:1
}),
List=ref([]),
keyword=ref()
const _shopgoodsList=()=>{
	shopgoodsList({
		page:PageData.page,
		limit:10,
		keyword:keyword.value
	}).then(res=>{
		if(res.code==1){
			List.value = List.value.concat(res.data.goods.data)
			PageData.last_page = res.data.goods.last_page
		}
	})
}
const seach=()=>{
	PageData.page=1
	List.value=[]
	_shopgoodsList()
}
const _shopsetUpDown=(id:number|string,type:number|string)=>{
	uni.showModal({
			title:'提示',
			content:'是否'+(type==1?'下架':'上架')+'商品?',
			success: function (res) {
					if (res.confirm) {
						shopsetUpDown({id:id}).then(res=>{
							if(res.code==1){
								List.value.forEach((item,index)=>{
									if(item.id==id){
										if(type==1){
											item.status = 0
										}else{
											item.status = 1
										}
									}
								})
							}
							uni.showToast({
									icon: 'none',
									title:res.msg
								})
						})
					} else if (res.cancel) {
					}
				}
		})
}
const _shopdelGoods=(id:number|string)=>{
	uni.showModal({
			title:'提示',
			content:'是否删除商品?',
			success: function (res) {
					if (res.confirm) {
						shopdelGoods({id:id}).then(res=>{
							if(res.code==1){
								List.value = List.value.filter(item=>item.id!=id)
							}
							uni.showToast({
								icon: 'none',
								title:res.msg
							})
						})
					} else if (res.cancel) {
					}
				}
		})
}
</script>

<style lang="scss">
@import 'goodsmanage.scss';
</style>
